#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main()
{
    int fd = open("/dev/tty", O_RDWR);   // 阻塞打开
    // int fd = open("/dev/tty", O_RDWR | O_NONBLOCK);      // 非阻塞打开

    int flags = fcntl(fd, F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);

    char buf[256];
    int ret = 0;

    while(1) {
        ret = read(fd, buf, sizeof(buf));
        if(ret < 0) {
            perror("read error: ");
            printf("ret = %d\n", ret);
        }
        
        if(ret) {
            printf("buf is: %s\n", buf);
        }
        
        printf("\n德玛西亚\n");
        sleep(1);
    }

    close(fd);
    return 0;
}

